package com.db.williamchart;

import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.Typeface;

/**
 * The Paint class holds the paint object used to draw charts
 * and provides methods to speed up its customization before drawing.
 */
public class Painter {

    private Paint paint;
    private Typeface labelsFont;

    public Painter(Paint paint, Typeface labelsFont) {
        this.labelsFont = labelsFont;
        this.paint = paint;
        this.paint.setTextAlign(Paint.Align.CENTER);
    }

    public void setLabelsFont(Typeface labelsFont) {
        this.labelsFont = labelsFont;
    }

    public Painter(Typeface labelsFont) {
        this.paint = new Paint();
        this.labelsFont = labelsFont;
        this.paint.setTextAlign(Paint.Align.CENTER);
    }

    /**
     * Measure text width given a certain font size.
     *
     * @param text     text to be measured.
     * @param textSize size used in font.
     * @return width of the text.
     */
    public float measureLabelWidth(String text, float textSize) {
        paint.setTypeface(labelsFont);
        paint.setTextSize(textSize);
        return paint.measureText(text);
    }

    /**
     * Measure the maximum text height for a given font size.
     *
     * @param textSize size used in font.
     * @return height of the text.
     */
    public float measureLabelHeight(float textSize) {
        paint.setTextSize(textSize);
        return paint.descent() - paint.ascent();
    }

    public float measureLabelAscent(float textSize) {
        paint.setTextSize(textSize);
        return paint.ascent();
    }

    public float measureLabelDescent(float textSize) {
        paint.setTextSize(textSize);
        return paint.descent();
    }

    /**
     * Prepares a Paint object already configured to be used.
     *
     * @param textSize    new font size to set in the paint.
     * @param color       new color (including alpha) to set in the paint.
     * @param style       new style to set in the paint.
     * @param strokeWidth new stroke width, used when style is Stroke or StrokeAndFill.
     * @param shader      May be null. New shader to be set in the paint.
     * @param font        May be null. New Typeface to be set in the paint.
     * @return Configured Paint object.
     */
    public Paint prepare(float textSize,
                         int color,
                         Paint.Style style,
                         float strokeWidth,
                         Shader shader,
                         Typeface font) {

        paint.setTextSize(textSize);
        paint.setColor(color);
        paint.setStyle(style);
        paint.setStrokeWidth(strokeWidth);
        paint.setShader(shader);
        paint.setTypeface(font);
        return paint;
    }

    public Paint getPaint() {
        return paint;
    }

//    public Paint setTextSize(float textSize){
//        paint.setTextSize(textSize);
//        return paint;
//    }
//
//    public Paint setTypeface(Typeface font){
//        paint.setTypeface(font);
//        return paint;
//    }
//
//    public Paint setColor(int color){
//        paint.setColor(color);
//        return paint;
//    }
//
//    public Paint setStyle(Paint.Style style){
//        paint.setStyle(style);
//        return paint;
//    }

    public Paint prepare(Shader shader,
                         Typeface font) {

        paint.setTextSize(15f);
        paint.setColor(-0x1000000);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(4f);
        paint.setShader(shader);
        paint.setTypeface(font);
        return paint;
    }

    public Paint prepare(
            int color,
            Paint.Style style
    ) {
        paint.setTextSize(15f);
        paint.setColor(color);
        paint.setStyle(style);
        paint.setStrokeWidth(4f);
        paint.setShader(null);
        paint.setTypeface(null);
        return paint;
    }

    public Paint prepare(
            float textSize,
            int color,
            Typeface font
    ) {
        paint.setTextSize(textSize);
        paint.setColor(color);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(4f);
        paint.setShader(null);
        paint.setTypeface(font);
        return paint;
    }

    public Paint prepare(int color, Paint.Style style, float strokeWidth) {
        paint.setTextSize(15f);
        paint.setColor(color);
        paint.setStyle(style);
        paint.setStrokeWidth(strokeWidth);
        paint.setShader(null);
        paint.setTypeface(null);
        return paint;
    }

    public Paint prepare(Shader shader,
                         Paint.Style style) {

        paint.setTextSize(15f);
        paint.setColor(-0x1000000);
        paint.setStyle(style);
        paint.setStrokeWidth(4f);
        paint.setShader(shader);
        paint.setTypeface(null);
        return paint;
    }
}
